package com.android.syssetting.models

import android.content.Context
import android.os.Build
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.camera.base.BaseApp
import com.camera.base.v.models.BaseViewModel
import com.camera.base.v.models.SystemNavBarViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import java.lang.reflect.Method
import javax.inject.Inject


@HiltViewModel
class SysInfoModel @Inject constructor(
)  : BaseViewModel(){
    val systemNavBarViewModel: SystemNavBarViewModel by lazy {
        SystemNavBarViewModel.getInstance(BaseApp.getApplication())
    }
    val imei1Visibility: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>()
    }
    val imei1Text: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
    val imei2Visibility: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>()
    }
    val imei2Text: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
    val snText: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
    val iccid1Visibility: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>()
    }
    val iccid2Visibility: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>()
    }
    val iccid1Text: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
    val iccid2Text: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
    init {
        imei1Visibility.value = View.GONE
        imei2Visibility.value = View.GONE
        iccid1Visibility.value = View.GONE
        iccid2Visibility.value = View.GONE
        try {
            val telephonyMgr= BaseApp.getApplication().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                iccid1Visibility.value = View.VISIBLE
                iccid1Text.value = telephonyMgr.simSerialNumber
            }
//            var uiccCardInfos = telephonyMgr.uiccCardsInfo
////            Log.e("TAG", ":2222222222222221"+  telephonyMgr.)
//            uiccCardInfos.forEach {
//                Log.e("TAG", ":222222222222222"+  it.iccId)
//            }
            var sm = SubscriptionManager.from(BaseApp.getApplication());
            var subscriptionInfoList = sm.activeSubscriptionInfoList

//            Log.e("TAG", ":222222222222222333"+  sm.getActiveSubscriptionInfoForSimSlotIndex(1).toString())
//            for ((index, subscriptionInfo) in subscriptionInfoList.withIndex()) {
//                Log.e("TAG", ":222222222222222333"+  subscriptionInfo.toString())
//                if (index == 0){
//                    iccid1Visibility.value = View.VISIBLE
//                    iccid1Text.value = subscriptionInfo.iccId
//                }else if (index == 1){
//                    iccid2Visibility.value = View.VISIBLE
//                    iccid2Text.value = subscriptionInfo.iccId
//                }
//            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (telephonyMgr.phoneCount > 0){
                    for (i in 0 .. telephonyMgr.phoneCount) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            if (i == 0){
                                imei1Visibility.value = View.VISIBLE
                                imei1Text.value = telephonyMgr.getImei(0)
                            }else if (i == 1){
                                imei2Visibility.value = View.VISIBLE
                                imei2Text.value = telephonyMgr.getImei(1)
                            }
                        } else {
                        }
                    }
                }
            }
            //imei
        } catch (e: Exception) {
            e.printStackTrace()
        }
        snText.value = getDeviceSN()

    }

    fun getDeviceSN(): String? {
        var serial: String? = null
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get: Method = c.getMethod("get", String::class.java)
            serial = get.invoke(c, "ro.serialno").toString()
            Log.e("TAG", "getDeviceSN: $serial")
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return serial
    }
}